﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.IO;

namespace HRM.Web
{
    public partial class gen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            const string CS_Ascx_Folder = "~/HRM/controls";
            string sPath;
            if (!Page.IsPostBack)
            {
                sPath = Page.MapPath(CS_Ascx_Folder);
                DirectoryInfo dirInfo = new DirectoryInfo(sPath);
                FileInfo[] arrFiles = dirInfo.GetFiles("*.ascx", SearchOption.AllDirectories);
                //Cau truc thu muc chi co 2 cap thoi nen dung thac mac la sao khong dung Recusive :D
                int DoDaiDuongDanGoc = Server.MapPath("~/").Length - 1;
                for (int j = 0; j < arrFiles.Length; j++)
                {
                    FileInfo sFile = arrFiles[j];
                    string duongdan = sFile.FullName.Substring(DoDaiDuongDanGoc);
                    duongdan = "~" + duongdan.Replace("\\", "/");
                    cboAscxList.Items.Add(new ListItem(duongdan.Replace(CS_Ascx_Folder + "/", "").Replace("/", " -> "), duongdan.ToLower()));
                }
                cboAscxList.Attributes["onchange"] = "javascript:setAscxPath();";
            }
            sPath = Request.Form["hdfAscxPath"];
            if (Request.QueryString["index"] != null)
            {
                sPath = cboAscxList.Items[int.Parse(Request.QueryString["index"])].Value;
                cboAscxList.SelectedIndex = int.Parse(Request.QueryString["index"]);
                if (!HttpContext.Current.IsDebuggingEnabled && Page.User.Identity.IsAuthenticated == false)
                    sPath = "~/HRM/controls/Home.ascx";
                else
                {
                    //App_Menu_Info objMenu = App_Menu_Controller.Instance().SelectBySrc(sPath);
                    //if (objMenu != null)
                    //    lblTenChucNang.InnerText = objMenu.Menu_Name;
                }
            }
            else
            {
                if (HttpContext.Current.IsDebuggingEnabled)
                    switch (Request.QueryString["ctl"])
                    {
                        case "msg":
                            sPath = "~/Common/Control/MsgSimulate.ascx";
                            break;
                        case "task":
                            sPath = "~/Common/Control/TaskManage.ascx";
                            break;
                        default:
                            sPath = "~/Common/Control/ClassGenerate.ascx";
                            break;
                    }
                else
                {
                    switch (Request.QueryString["ctl"])
                    {
                        case "menu":
                            sPath = "~/PKH/controls/HeThong/Menu_QuanTri.ascx";
                            break;
                        default:
                            if (Request.QueryString["ctl"] == "login") FormsAuthentication.SignOut();
                            sPath = "~/HRM/controls/Home.ascx";
                            break;
                    }
                }
            }
            if (sPath != null && sPath != "")
            {
                if (!sPath.StartsWith("~")) sPath = "~" + sPath;
                plhAscx.Controls.Add(Page.LoadControl(sPath));
            }
            Page.ClientScript.RegisterHiddenField("hdfAscxPath", sPath);
            //
            divDebug.Visible = HttpContext.Current.IsDebuggingEnabled;
            //
            if (Page.User.Identity.IsAuthenticated)
                Page.ClientScript.RegisterStartupScript(this.GetType(), "Timeout", "window.setTimeout(function(){document.location = '/default.aspx?ctl=login';}," + ((20 * 60 + 1) * 1000).ToString() + ");", true);
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            FormsAuthentication.SignOut();
            FormsAuthentication.RedirectFromLoginPage(txtUser_Name.Text, false);
        }
    }
}